/*
 * Copyright (C) 2010 Cyril Mottier (http://www.cyrilmottier.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hktf.uilib.app;

import android.app.ActivityManager;
import android.content.Context;

import com.hktf.uilib.utils.WxsdkUtil;
import com.tencent.bugly.crashreport.CrashReport;
import com.hktf.corelib.BaseApplication;
import com.hktf.corelib.exception.CrashHandler;
import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.Config;
import com.hktf.corelib.utils.LogMds;
import com.hktf.corelib.utils.MMKVUtils;
import com.hktf.corelib.utils.OKGoUtil;
import com.hktf.corelib.utils.SharePreUtils;
import com.hktf.uilib.utils.SoundPlayUtils;
import com.umeng.commonsdk.UMConfigure;

/**
 * Define various methods that should be overridden in order to style your
 * application.
 *
 * @author Cyril Mottier
 */
public class UiApplication extends BaseApplication {


    public static Context applicationContext;
    private static UiApplication instance;


    @Override
    public void onCreate() {
        super.onCreate();


    }
    private static final String UM_APPKEY="68abc2b6ec2b5b6f8826de05";

    protected void initApp() {
        //在主进程初始化
        if (!getApplicationContext().getPackageName().equals(getCurrentProcessName())) {
            return;
        }
        //友盟预初始化
        UMConfigure.preInit(getContext(),UM_APPKEY,"hktf");
        OKGoUtil.init(this);
        SoundPlayUtils.init(CommUtils.getContext());

        applicationContext = this;
        instance = this;

        Config.islandscape = SharePreUtils.getScreenState(this, SharePreUtils.SCREENSTATE, CommUtils.getOrientationConfig());
        LogMds.e("cdz","Config.islandscape="+ Config.islandscape);

    }

    public static void initOtherSdk(){

        MMKVUtils.init(getContext());
        getInstance().initService();

        Config.updateAtsStorage();

        //hxSDKHelper.onInit(applicationContext);
        // 异常崩溃异常信息保存
        CrashHandler handler = CrashHandler.getInstance();
        handler.init(getContext());

        initUM();


        String userid = "";
        String telphone = "";
        if (Config.USERINFO != null) {
            userid = Config.USERINFO.getUserId();
        }
        CrashReport.initCrashReport(getInstance(), "602843c639", false);
        CrashReport.setUserId(userid);

//        WxsdkUtil.regToWx();
    }

    private static void initUM() {
//        ApplicationInfo appInfo = null;
//        try {
//            appInfo = getContext().getPackageManager().getApplicationInfo(getContext().getPackageName(),
//                    PackageManager.GET_META_DATA);
//        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
//        }
//        Bundle metaData = null;
//        if (appInfo != null) {
//            metaData = appInfo.metaData;
//        }
//        if (metaData != null) {
//            String UMENG_APPKEY = metaData.getString("UMENG_APPKEY");
//            String UMENG_CHANNEL = metaData.getString("UMENG_CHANNEL");
//            LogMds.i("cdz", "UMENG_APPKEY=" + UMENG_APPKEY);
//            LogMds.i("cdz", "UMENG_CHANNEL=" + UMENG_CHANNEL);
//            UMConfigure.setLogEnabled(Config.LOG_OFF_DEBUG);
//            UMConfigure.setProcessEvent(true);
//            if (UMENG_APPKEY != null && UMENG_CHANNEL != null) {
//                UMConfigure.preInit(getContext(), UMENG_APPKEY, UMENG_CHANNEL);
//                UMConfigure.init(getContext(), UMENG_APPKEY, UMENG_CHANNEL, UMConfigure.DEVICE_TYPE_PHONE, "");
//            }
//        }

        UMConfigure.init(getContext(),UM_APPKEY,"hktf",UMConfigure.DEVICE_TYPE_PHONE,"");



    }

    /**
     * 获取当前进程名
     */
    private String getCurrentProcessName() {
        int pid = android.os.Process.myPid();
        String processName = "";
        ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService
                (Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
            if (process.pid == pid) {
                processName = process.processName;
            }
        }
        return processName;
    }

    public static UiApplication getInstance() {
        return instance;
    }


}
